package cn.dengta.webapp.trade.entity;

import java.math.BigDecimal;
import java.util.Date;
import javax.annotation.Nullable;

import cn.dengta.common.model.VerifyStatus;
import cn.dengta.webapp.base.entity.BaseEntity;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import cn.dengta.webapp.trade.model.TradeType;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * 邀请奖励日志表
 *
 * @author lgy
 * @since 2022-07-14
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("tr_invite_integral_log")
public class InviteIntegralLog extends TimeBasedEntity {

	/** 关联的用户ID */
	private Long userId;
	/** 本次交易的关联方用户ID */
	private Long toUserId;
	/** 收益类型 */
	private String coinTradeType;
	/** 邀请奖励 */
	private BigDecimal tradeAmount;
	/** 交易积分 */
	private BigDecimal totalAmount;
	/** 邀请奖励比例% */
	private BigDecimal tradeRate;
	/** 关联的业务实体类型 */
	private String assocType;
	/** 关联的业务实体ID */
	private Long assocId;
	/** 关联消费者的钻石日志记录ID（便于反查） */
	@Nullable
	private Long toUserCoinLogId;
	/** 状态：-1=审核拒绝；0=待审核；1=审核通过 */
	private Integer status;
	/** 邀请奖励ID */
	private Long inviteRewardId;
	/** 备注 */
	private String remark;

	protected transient BaseEntity assocObj;

	public InviteIntegralLog init(Long userId, Long toUserId, TradeType coinTradeType, BigDecimal tradeAmount, BigDecimal totalAmount, BigDecimal tradeRate, BaseEntity obj, @Nullable Long toUserCoinLogId, String remark, Date now) {
		this.userId = userId;
		this.toUserId = toUserId;
		this.coinTradeType = X.map(coinTradeType, TradeType::getValue);
		this.tradeAmount = tradeAmount;
		this.totalAmount = totalAmount;
		this.tradeRate = tradeRate;
		handleAssocObject(obj);
		this.toUserCoinLogId = toUserCoinLogId;
		this.status = VerifyStatus.WAIT_VERIFY.getValue();
		this.remark = StringUtil.toString(remark);
		super.initTime(now);
		return this;
	}

	public void handleAssocObject(BaseEntity obj) {
		this.assocObj = obj;
		this.assocType = obj.entityName();
		this.assocId = obj.getId();
	}

	public static final String USER_ID = "user_id";
	public static final String TO_USER_ID = "to_user_id";
	public static final String COIN_TRADE_TYPE = "coin_trade_type";
	public static final String TRADE_AMOUNT = "trade_amount";
	public static final String TOTAL_AMOUNT = "total_amount";
	public static final String TRADE_RATE = "trade_rate";
	public static final String ASSOC_TYPE = "assoc_type";
	public static final String ASSOC_ID = "assoc_id";
	public static final String TO_USER_COIN_LOG_ID = "to_user_coin_log_id";
	public static final String STATUS = "status";
	public static final String INVITE_REWARD_ID = "invite_reward_id";
	public static final String REMARK = "remark";

}
